package com.qhit.dao.impl;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.qhit.dao.StudentDao;
import com.qhit.entity.Student;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * TODO
 *
 * @author 村头老杨头
 * @version 1.0
 * @date 2021/10/12 9:45
 */
public class StudentDaoImpl implements StudentDao {
    //做增删改查的工具类       参数数据源
    private QueryRunner queryRunner = new QueryRunner(new ComboPooledDataSource());
    //查所有学生
    @Override
    public List<Student> getStudentList() throws SQLException {
        String sql = "select * from student";
        List<Student> query = queryRunner.query(sql, new BeanListHandler<Student>(Student.class));

        return query;
    }


    //根据学号查学生
    @Override
    public Student getStudentById(String Sno) throws SQLException {
        String sql = "select * from student where sno=?";
        Student query = queryRunner.query(sql, new BeanHandler<Student>(Student.class), Sno);
        return query;
    }
    //增加学生
    @Override
    public int addStudent(Student student) throws SQLException {
        String sql = "insert into student  values (?,?,?,?,?)";
        return queryRunner.update(sql,student.getSno(),student.getSname(),student.getSsex(),student.getSbirthday(),student.getClazz());
    }

    @Override
    public int deleteStudnet(String sno) throws SQLException {
        String sql = "delete from student where sno=?";
        return queryRunner.update(sql,sno);
    }
}
